<?php
/**
 * Test
 */
namespace App\Http\Controllers\Test;

use App\Models\Users;
use DB;

class PumaController {

    /**
     * puma统计
     */
    public function map() {
        //统计一共有多少会员
        //$totalUserCount = Users::all()->count();
        $totalUser = DB::select('select count(*) as totalCount from users');
        $totalUserCount = $totalUser[0]->totalCount;

        //各个城市有多少会员
        $chengdu = Users::where('position', '成都')->count();
//        $chongqing = Users::where('position', '重庆')->count();
//        $beijing = Users::where('position', '北京')->count();
//        $shanghai = Users::where('position', '上海')->count();
//        $guangzhou = Users::where('position', '广州')->count();
//        $neijiang = Users::where('position', '内江')->count();
//        $totalUserChengdu = DB::select('select count(*) as totalCount from users where position=:position', [':position' => '成都']);
//        $totalUserCountChengdu = $totalUserChengdu[0]->totalCount;
        $data = [
            'total' => $totalUserCount,
            'chengdu' => $chengdu,
            'chongqing' => $chongqing,
            'beijing' => $beijing,
            'shanghai' => $shanghai,
            'guangzhou' => $guangzhou,
            'neijiang' => $neijiang
        ];

        return view("test/puma", $data);
    }

    public function showTest() {

    }

    public function test() {
//        $totalUser = DB::select('select position,count(*) as num from users where position != \'\' group by position');
//        $newTotal = [];
//        foreach ($totalUser as $item) {
//            //排除一些垃圾数据
////            if (preg_match_all("/^([\x81-\xfe][\x40-\xfe])+$/", $item->position, $match)) {
////                $newTotal[$item->position] = $item->num;
////            }
//
//            if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $item->position) > 0) {
//                $newTotal[$item->position] = $item->num;
//            }
//
//
//        }
//        print_r($newTotal);
//        die("hello");
        return $this->sichuan();
    }

    //
    public function sichuan() {
        $pos = '"成都","德阳","绵阳","攀枝花","遂宁","南充","广元","乐山","宜宾","泸州","达州","广安","巴中","雅安","内江","自贡","资阳","眉山","江油","阆中","华蓥","万源","崇州","简阳","西昌","什邡","彭州","峨眉山","都江堰","邛崃","广汉"';
        //$pos = '成都,德阳,绵阳,攀枝花,遂宁,南充,广元,乐山,宜宾,泸州,达州,广安,巴中,雅安,内江,自贡,资阳,眉山,江油,阆中,华蓥,万源,崇州,简阳,西昌,什邡,彭州,峨眉山,都江堰,邛崃,广汉';
        $totalUser = DB::select('select position,count(*) as num from users where position in("成都","德阳","绵阳","攀枝花","遂宁","南充","广元","乐山","宜宾","泸州","达州","广安","巴中","雅安","内江","自贡","资阳","眉山","江油","阆中","华蓥","万源","崇州","简阳","西昌","什邡","彭州","峨眉山","都江堰","邛崃","广汉") group by position');
        $newTotal = [];
        $data = [
            'posKeyList' => '',
            'posValueList' => ''
        ];
//        foreach ($totalUser as $item) {
//            $newTotal[$item->position] = $item->num;
//        }
        foreach ($totalUser as $item) {
            $data['posKeyList'] = $data['posKeyList'] . '"' . $item->position . '",';
            $data['posValueList'] = $data['posValueList'] . $item->num . ',';
        }

        $data['posKeyList'] = trim($data['posKeyList'], ',');
        $data['posValueList'] = trim($data['posValueList'], ',');


        return view("test/puma/test", $data);
    }
}